package com.service.impl;

import com.core.base.BaseMapper;
import com.core.base.BaseService;
import com.core.response.ResponseResult;
import com.mapper.jpa.UsersRepository;
import com.model.jpa.Users;
import com.service.IUsersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("iUsersService")
public class UsersServiceImpl extends BaseService<Users,Long> implements IUsersService {

    @Autowired
    private UsersRepository usersRepository;

    @Override
    public BaseMapper<Users, Long> getMapper() {
        return usersRepository;
    }

    /**1.登陆方法,未设置md5加密**/
    public ResponseResult login(String userName,String password){
        Users users =usersRepository.findByUserName(userName);
        if(users==null){
            return ResponseResult.error("用户名输入错误");
        }
        users = usersRepository.findByUserNameAndPassword(userName,password);
        if(users==null){
            return ResponseResult.error("密码错误");
        }
        return ResponseResult.successMsg("登陆成功",users);

    }

    /**2.增加一名users**/
    public ResponseResult addUsers(Users users){
       Users rowCount = usersRepository.save(users);
       if(rowCount==null){
           return ResponseResult.error("添加用户失败");
       }
       return ResponseResult.success();
    }

    /**3.根据名字查出一名users**/
    public ResponseResult getByUserName(String userName){
        if(userName==null){
            return ResponseResult.error("用户名不能为空");
        }
       Users users = usersRepository.findByUserName(userName);
        if(users == null){
            return ResponseResult.error("未查询到用户信息");
        }
       return ResponseResult.success(users);
    }

    /**根据id查询用户信息**/
    public Users getById(Long id){
        if (id!=null){
           return usersRepository.getById(id);
        }
        return null;
    }

    /**跟新users用户信息**/
    public ResponseResult updateUsersInfo(Users users){
        //查询出当前用户信息
        Users currentUsers = usersRepository.getById(users.getId());
        BeanUtils.copyProperties(users,currentUsers);
        Users updateUsers = usersRepository.saveAndFlush(currentUsers);
        if(updateUsers == null){
            return ResponseResult.error("跟新失败");
        }
        return ResponseResult.success(updateUsers);
    }

    /**根据id删除users**/
    public ResponseResult deleteUsers(Long[] ids){
        if(ids!=null){
            for(Long id:ids){
                usersRepository.deleteById(id);
            }
            return ResponseResult.success();
        }
        return ResponseResult.error("删除失败");
    }



    /****/
    public Page<Users> selectUsers(String realName,Pageable pageable){

        Page<Users> usersPage = usersRepository.findAllByRealName(pageable,realName);
        List<Users> usersList = usersPage.getContent();
        return new PageImpl<>(usersList);
    }
}
